package com.commerceerp.system.controller;


import com.alibaba.fastjson.JSONArray;
import com.commerceerp.system.entity.Constant;
import com.commerceerp.system.entity.ForderInfo;
import com.commerceerp.system.entity.PorderInfo;
import com.commerceerp.system.service.ForderService;
import com.commerceerp.system.service.PurchaseOrderService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.RdPage;
import com.commerceerp.system.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "财务接口", value = "财务接口")
@Scope("prototype")
@RestController
@EnableAutoConfiguration
public class ForderController {

    @Autowired
    private ForderService forderService;

    @ApiOperation(value = "根据不同状态获取财务单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "forderId", value = "流水号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "forderMoney", value = "金额", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "forderPorderId", value = "来源单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "forderStatus", value = "状态(1:待付款,2:已付款,3:待收款,4:已收款,5:驳回状态)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllForderInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllForderInfo(String forderId,BigDecimal forderMoney,String forderPorderId,String forderStatus,
                                      Integer current,Integer pageSize) throws IOException {


        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));
        ForderInfo forderInfo = new ForderInfo();
        forderInfo.setForderId(forderId);
        forderInfo.setForderMoney(forderMoney);
        forderInfo.setForderPorderId(forderPorderId);
        forderInfo.setForderStatus(forderStatus);
        List<Map> list = forderService.getAllForderInfoByStatus(forderInfo);
        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<Map>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

        return new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",list,rdPage);
    }


    @ApiOperation(value = "确定付款/确定收款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "forderId", value = "流水号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "frRemark", value = "备注说明", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "1:付款,2:收款", required = false, dataType = "String"),

    })
    @RequestMapping(value = "/updForderStatus", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updForderStatus( @RequestParam(value = "forderId", required = false) String forderId,
                                      @RequestParam(value = "type", required = false) String type,
                                      @RequestParam(value = "frRemark", required = false) String frRemark) throws IOException {

        ApiResult result = null;
        ForderInfo forderInfo = new ForderInfo();
        forderInfo.setForderId(forderId);

        if("1".equals(type)){

            forderInfo.setForderPayTime(DateUtil.getYMDHMSDate());
            forderInfo.setForderStatus("2");

            forderService.saveNewForderRecord(forderId,forderInfo.getForderPorderId(),forderInfo.getForderInOut(),forderInfo.getForderMoney(),frRemark);

        } else if("2".equals(type)){

            forderInfo.setForderReceivablesTime(DateUtil.getYMDHMSDate());
            forderInfo.setForderStatus("4");

            forderService.saveNewForderRecord(forderId,forderInfo.getForderPorderId(),forderInfo.getForderInOut(),forderInfo.getForderMoney(),frRemark);

        }
        Integer success = forderService.updForderStatus(forderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }



    @ApiOperation(value = "获取所有财务明细")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "frForderId", value = "流水号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "frPorderId", value = "采购单号", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "frMoney", value = "金额", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "frInOut", value = "1:付款，2:收款", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "截止时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllForderRecord", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllForderRecord( @RequestParam(value = "frForderId", required = false) String frForderId,
                                       @RequestParam(value = "frPorderId", required = false) String frPorderId,
                                       @RequestParam(value = "frMoney", required = false) BigDecimal frMoney,
                                       @RequestParam(value = "frInOut", required = false) String frInOut,
                                       @RequestParam(value = "beginTime", required = false) String beginTime,
                                       @RequestParam(value = "endTime", required = false) String endTime,
                                       @RequestParam(value = "current") Integer current,
                                       @RequestParam(value = "pageSize") Integer pageSize) throws IOException {

        ApiResult result = null;
        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));


        List<Map> list = forderService.getAllForderRecord(frForderId,frPorderId,frMoney,frInOut,beginTime,endTime);

        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<Map>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",list,rdPage);

        return result;
    }


    @ApiOperation(value = "驳回采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "forderId", value = "采购单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "forderRejectReason", value = "驳回原因", required = false, dataType = "String"),

    })
    @RequestMapping(value = "/updForderToReject", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updForderToReject( @RequestParam(value = "forderId", required = false) String forderId,
                                      @RequestParam(value = "forderRejectReason", required = false) String forderRejectReason) throws IOException {

        ApiResult result = null;
        ForderInfo forderInfo = new ForderInfo();
        forderInfo.setForderId(forderId);


            forderInfo.setForderStatus("5");
            forderInfo.setForderId(forderId);
            forderInfo.setForderRejectReason(forderRejectReason);
            forderInfo.setForderRejectTime(DateUtil.getYMDHMSDate());

            Integer success= forderService.updForderToReject(forderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "财务页面各状态数量")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/getCountByForderStatus", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getCountByForderStatus( ) throws IOException {


        return new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",forderService.getCountByForderStatus(),null);
    }


    @ApiOperation(value = "财务明细的支出，收入，利润")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "frForderId", value = "流水号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "frPorderId", value = "采购单号", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "frMoney", value = "金额", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "frInOut", value = "1:付款，2:收款", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "截止时间", required = false, dataType = "String")
    })
    @RequestMapping(value = "/getAllInOutMoney", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllInOutMoney(@RequestParam(value = "frForderId", required = false) String frForderId,
                                            @RequestParam(value = "frPorderId", required = false) String frPorderId,
                                            @RequestParam(value = "frMoney", required = false) BigDecimal frMoney,
                                            @RequestParam(value = "frInOut", required = false) String frInOut,
                                            @RequestParam(value = "beginTime", required = false) String beginTime,
                                            @RequestParam(value = "endTime", required = false) String endTime ) throws IOException {

        ApiResult result = null;

        Map<String,Object> map = forderService.getAllInOutMoney(frForderId,frPorderId,frMoney,frInOut,beginTime,endTime);

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",map,null);

        return result;
    }



}
